#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QtCore/QCoreApplication>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    reset();

    // Set Configuration Options Here

    // Start Session Here
    // BangoAgent::onStartSession([Your Application ID]);
}

MainWindow::~MainWindow()
{
    BangoAgent::onEndSession();
    delete ui;
}

void MainWindow::setOrientation(ScreenOrientation orientation)
{
#if defined(Q_OS_SYMBIAN)
    // If the version of Qt on the device is < 4.7.2, that attribute won't work
    if (orientation != ScreenOrientationAuto) {
        const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
        if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
            qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
            return;
        }
    }
#endif // Q_OS_SYMBIAN

    Qt::WidgetAttribute attribute;
    switch (orientation) {
#if QT_VERSION < 0x040702
    // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
    case ScreenOrientationLockPortrait:
        attribute = static_cast<Qt::WidgetAttribute>(128);
        break;
    case ScreenOrientationLockLandscape:
        attribute = static_cast<Qt::WidgetAttribute>(129);
        break;
    default:
    case ScreenOrientationAuto:
        attribute = static_cast<Qt::WidgetAttribute>(130);
        break;
#else // QT_VERSION < 0x040702
    case ScreenOrientationLockPortrait:
        attribute = Qt::WA_LockPortraitOrientation;
        break;
    case ScreenOrientationLockLandscape:
        attribute = Qt::WA_LockLandscapeOrientation;
        break;
    default:
    case ScreenOrientationAuto:
        attribute = Qt::WA_AutoOrientation;
        break;
#endif // QT_VERSION < 0x040702
    };
    setAttribute(attribute, true);
}

void MainWindow::showExpanded()
{
#ifdef Q_OS_SYMBIAN
    showMaximized();
#elif defined(Q_WS_MAEMO_5)
    showMaximized();
#else
    show();
#endif
}

QString MainWindow::getEventName()
{
   return ui->txtEventName->text().trimmed();
}

QString MainWindow::getEventDetails()
{
   return ui->txtEventDetail->text().trimmed();
}

QString MainWindow::getEventValue()
{
   return ui->txtEventValue->text().trimmed();
}

QMap<QString,QString> MainWindow::getEventParameters()
{
    QMap<QString, QString> eventParameters;
    if(ui->txtParamName1->text().trimmed().size() > 0)
    {
        eventParameters[ui->txtParamName1->text().trimmed()] =  ui->txtParamValue1->text().trimmed();
    }

    if(ui->txtParamName2->text().trimmed().size() > 0)
    {
        eventParameters[ui->txtParamName2->text().trimmed()] =  ui->txtParamValue2->text().trimmed();
    }
    return eventParameters;
}

void MainWindow::reset()
{
         ui->txtEventName->setText("QT Event");
         ui->txtEventDetail->setText("QT Event Detail");
         ui->txtEventValue->setText("QT Event Value");
         ui->txtParamName1->setText("BGO_SOURCE");
         ui->txtParamValue1->setText("QT SOURCE");
         ui->txtParamName2->setText("BGO_TERM");
         ui->txtParamValue2->setText("QT TERM");

}

void MainWindow::clear()
{
         ui->txtEventName->setText("");
         ui->txtEventDetail->setText("");
         ui->txtEventValue->setText("");
         ui->txtParamName1->setText("");
         ui->txtParamValue1->setText("");
         ui->txtParamName2->setText("");
         ui->txtParamValue2->setText("");
}

void MainWindow::on_btnReset_clicked()
{
    reset();
}

void MainWindow::on_btnClear_clicked()
{
     clear();
}

void MainWindow::on_btnLiveVideoStart_clicked()
{
    switch (getOverload()) {
    case 1:
            BangoAgent::onLiveVideoStart();
            break;
    case 2:
            BangoAgent::onLiveVideoStart(getEventValue());
            break;
    case 3:
            BangoAgent::onLiveVideoStart(getEventValue(), getEventDetails());
            break;
    case 4:
            BangoAgent::onLiveVideoStart(getEventValue(), getEventParameters());
            break;
    default:
            BangoAgent::onLiveVideoStart(getEventValue(), getEventDetails(),
                            getEventParameters());
            break;
    }
}

void MainWindow::on_btnLiveVideoStop_clicked()
{
    switch (getOverload()) {
    case 1:
            BangoAgent::onLiveVideoStop();
            break;
    case 2:
            BangoAgent::onLiveVideoStop(getEventValue());
            break;
    case 3:
            BangoAgent::onLiveVideoStop(getEventValue(), getEventDetails());
            break;
    case 4:
            BangoAgent::onLiveVideoStop(getEventValue(), getEventParameters());
            break;
    default:
            BangoAgent::onLiveVideoStop(getEventValue(), getEventDetails(),
                            getEventParameters());
            break;
    }
}

void MainWindow::on_btnSearch_clicked()
{
    switch (getOverload()) {
    case 1:
            BangoAgent::onSearch();
            break;
    case 2:
            BangoAgent::onSearch(getEventValue());
            break;
    case 3:
            BangoAgent::onSearch(getEventValue(), getEventDetails());
            break;
    case 4:
            BangoAgent::onSearch(getEventValue(), getEventParameters());
            break;
    default:
            BangoAgent::onSearch(getEventValue(), getEventDetails(),
                            getEventParameters());
            break;
    }
}

void MainWindow::on_btnOnDemandVideoStart_clicked()
{
    switch (getOverload()) {
    case 1:
            BangoAgent::onDemandVideoStart();
            break;
    case 2:
            BangoAgent::onDemandVideoStart(getEventValue());
            break;
    case 3:
            BangoAgent::onDemandVideoStart(getEventValue(), getEventDetails());
            break;
    case 4:
            BangoAgent::onDemandVideoStart(getEventValue(), getEventParameters());
            break;
    default:
            BangoAgent::onDemandVideoStart(getEventValue(), getEventDetails(),
                            getEventParameters());
            break;
    }
}

void MainWindow::on_btnOnDemandVideoStop_clicked()
{
    switch (getOverload()) {
    case 1:
            BangoAgent::onDemandVideoStop();
            break;
    case 2:
            BangoAgent::onDemandVideoStop(getEventValue());
            break;
    case 3:
            BangoAgent::onDemandVideoStop(getEventValue(), getEventDetails());
            break;
    case 4:
            BangoAgent::onDemandVideoStop(getEventValue(), getEventParameters());
            break;
    default:
            BangoAgent::onDemandVideoStop(getEventValue(), getEventDetails(),
                            getEventParameters());
            break;
    }
}

void MainWindow::on_btnPageView_clicked()
{
    switch (getOverload()) {
    case 1:
            BangoAgent::onPageView();
            break;
    case 2:
            BangoAgent::onPageView(getEventValue());
            break;
    case 3:
            BangoAgent::onPageView(getEventValue(), getEventDetails());
            break;
    case 4:
            BangoAgent::onPageView(getEventValue(), getEventParameters());
            break;
    default:
            BangoAgent::onPageView(getEventValue(), getEventDetails(),
                            getEventParameters());
            break;
    }
}

void MainWindow::on_btnIdle_clicked()
{
    switch (getOverload()) {
    case 1:
            BangoAgent::onIdle();
            break;
    case 2:
            BangoAgent::onIdle(getEventValue());
            break;
    case 3:
            BangoAgent::onIdle(getEventValue(), getEventDetails());
            break;
    case 4:
            BangoAgent::onIdle(getEventValue(), getEventParameters());
            break;
    default:
            BangoAgent::onIdle(getEventValue(), getEventDetails(),
                            getEventParameters());
            break;
    }
}

void MainWindow::on_btnResume_clicked()
{
    switch (getOverload()) {
    case 1:
            BangoAgent::onResume();
            break;
    case 2:
            BangoAgent::onResume(getEventValue());
            break;
    case 3:
            BangoAgent::onResume(getEventValue(), getEventDetails());
            break;
    case 4:
            BangoAgent::onResume(getEventValue(), getEventParameters());
            break;
    default:
            BangoAgent::onResume(getEventValue(), getEventDetails(),
                            getEventParameters());
            break;
    }
}

void MainWindow::on_btnLog_clicked()
{
    switch (getOverload()) {
    case 1:
            BangoAgent::onEvent(getEventName());
            break;
    case 2:
            BangoAgent::onEvent(getEventName(), getEventValue());
            break;
    case 3:
            BangoAgent::onEvent(getEventName(), getEventValue(), getEventDetails());
            break;
    case 4:
            BangoAgent::onEvent(getEventName(), getEventValue(), getEventParameters());
            break;
    default:
            BangoAgent::onEvent(getEventName(), getEventValue(), getEventDetails(),
                            getEventParameters());
            break;
    }
}

void MainWindow::on_btnError_clicked()
{
    BangoAgent::onError("TestError", "QT Test App", "This is my test error");
}

void MainWindow::on_btnPostNow_clicked()
{
    BangoAgent::PostEvents();
}

void MainWindow::on_btnViewBatch_clicked()
{
    qDebug("\n---Start Batch--\n");
    qDebug(BangoAgent::ViewBatch().toAscii());
    qDebug("\n---End Batch--\n");
}


int MainWindow::getOverload() {
        /*
         * 1 - Only Event Name Provided
         * 2 - Event name and value provided
         * 3 - Event name, value and detail provided
         * 4 - Event name, value and event parameters provided
         * 5 - Event name, value, detail and event parameters provided
         */

        QString eventValue = getEventValue();
        QString eventDetail = getEventDetails();
        QMap<QString,QString> eventParameters = getEventParameters();

        if ((eventValue.length() <= 0)) {
                return 1;
        }

        if ((eventDetail.length() <= 0) && eventParameters.isEmpty()) {
                return 2;
        }

        if ((eventDetail.length() > 0) && eventParameters.isEmpty()) {
                return 3;
        }

        if ((eventDetail.length() <= 0) && !eventParameters.isEmpty()) {
                return 4;
        }

        if ((eventDetail.length() > 0) && !eventParameters.isEmpty()) {
                return 5;
        }

        return 1;
}
